package acheng1314.cn.a3dbuild2.service;

import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;

import com.acheng.achengutils.utils.SPHelper;

import acheng1314.cn.a3dbuild2.R;


/**
 * Created by Administrator on 2016/6/3/003.
 */
public class LocationService extends Service {
    private Location location;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //安卓版本<6.0
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null) {
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 50, new gspLocationListener());

            } else {
                saveLocation();
            }
            return super.onStartCommand(intent, flags, startId);
        }
        //安卓版本>6.0
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.

                location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location == null) {
                    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 50, new gspLocationListener());
                } else {
                    saveLocation();
                }
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    class gspLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                saveLocation();

            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }

    private void saveLocation() {
        SPHelper.setString(LocationService.this, getString(R.string.gpsValues), getString(R.string.latitude), location.getLatitude() + "");
        SPHelper.setString(LocationService.this, getString(R.string.gpsValues), getString(R.string.longitude), location.getLongitude() + "");
        SPHelper.setString(LocationService.this, getString(R.string.gpsValues), getString(R.string.altitude), location.getAltitude() + "");
        stopSelf();
    }
}
